﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseWebAPP.code.DAL;
using WarehouseWebAPP.code.DAL.ComponentDSTableAdapters;

namespace WarehouseWebAPP.code.BLL
{
    public class ComponentBLL
    {
        private componentTableAdapter ComponentAdapter = null;
        public componentTableAdapter Adapter
        {
            get
            {
                if (ComponentAdapter==null) ComponentAdapter=new componentTableAdapter();
                return ComponentAdapter;
            }
        }
        public void ComponentInsert(string component_serial_num, Guid component_vendor, string component_description,
            string component_place, int component_quantity, Guid component_supplier)
        {
            if (Adapter.ComponentInsert(component_serial_num, component_vendor, component_description, component_place, component_quantity, component_supplier) < 1)
                throw new Exception("Component was not added");
            //log event
            string[] Parameters = {component_serial_num, "Component" };
            MyEvent ev = History.EventDecorator(History.ActionType.Add, Parameters, DateTime.Now);
            History.AddEvent(ev);
        }

        public void ComponentDelete(Guid original_component_num)
        {
            if (original_component_num == null)
                throw new Exception ("Component Number is missing");

            string component_serial_num = ComponentGetByNum(original_component_num)[0].component_serial_num;
            if (Adapter.ComponentDelete(original_component_num) < 1)
                throw new Exception ("Component was not deleted");

            //log event
            string[] Parameters = {component_serial_num, "Component" };
            MyEvent ev = History.EventDecorator(History.ActionType.Delete, Parameters, DateTime.Now);
            History.AddEvent(ev);
        }

        public void ComponentUpdate(string component_serial_num, Guid component_vendor, string component_description,
            string component_place, int component_quantity, Guid component_supplier, Guid original_component_num)
        {
            if (original_component_num == null)
                throw new Exception("Component Number is missing");
            if (Adapter.ComponentUpdate(component_serial_num, component_vendor, component_description, component_place, component_quantity, component_supplier, original_component_num) < 1)
                throw new Exception("Component was not Updated");
            
            //log event
            string[] Parameters = { component_serial_num, "Component" };
            MyEvent ev = History.EventDecorator(History.ActionType.Update, Parameters, DateTime.Now);
            History.AddEvent(ev);
        }

        public ComponentDS.componentDataTable ComponentGetByPlace(string component_place)
        {
            if (component_place == null)
                throw new Exception("Component Place is missing");
            return Adapter.ComponentGetByPlace(component_place);
        }

        public ComponentDS.componentDataTable ComponentGetByVendor(Guid component_vendor)
        {
            if (component_vendor == null)
                throw new Exception("Vendor Number is missing");
            return Adapter.ComponentGetByVendor(component_vendor);
        }
        public ComponentDS.componentDataTable ComponentGetBySupplier(Guid component_supplier)
        {
            if (component_supplier == null)
                throw new Exception("Supplier Number is missing");
            return Adapter.ComponentGetBySupplier(component_supplier);
        }

        public ComponentDS.componentDataTable ComponentGetLessthanQuantity(int component_quantity)
        {
            if (component_quantity < 0)
                throw new Exception("Quantity should be greater than 0");
            return Adapter.ComponentGetLessThanQuantity(component_quantity);
        }

        public ComponentDS.componentDataTable ComponentGetByDescription(string component_description)
        {
            if (component_description == null)
                throw new Exception("Component description is missing");
            return Adapter.ComponentGetByDescription(component_description);
        }

        public ComponentDS.componentDataTable ComponentGetBySN(string component_serial_num)
        {
            //if(component_serial_num == null)
            //    throw new Exception("Component serial number is missing");
            return Adapter.ComponentGetBySerialNum(component_serial_num);
        }

        public ComponentDS.componentDataTable ComponentGetByNum(Guid component_num)
        {
            //if (component_num == null)
            //    throw new Exception("Component number is missing");
            return Adapter.ComponentGetByNum(component_num);
        }

                
        public ComponentDS.componentDataTable ComponentSearch (string component_serial_num, string component_vendor, string component_description,
           string component_place, int component_quantity, string component_supplier)
        {
            return Adapter.ComponentSearch(component_description, component_serial_num, component_place, component_vendor, component_quantity, component_supplier);
        }

        public int ComponentGetQuantityByNum(Guid component_num)
        {
            return (int)(Adapter.ComponentGetQuantityByNum(component_num));
        }

        public void ComponentQuantityUpdate (Guid component_num, int component_quantity)
        {
            if (component_quantity < 0)
                throw new Exception("Quantity should be greater than 0");
            Adapter.ComponentUpadateQuantity(component_quantity,component_num );
        }

        public ComponentDS.componentDataTable ComponentGetData()
        {
            return Adapter.GetData();
        }
    }
}
